package com.catering.controller.untilController;

import com.catering.service.utils.UntilService;
import com.catering.until.UploadResult;
import com.catering.until.file.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RestController
@RequestMapping("/ueditor")
@Slf4j
public class UeditorController {
	
	@Autowired
	private UntilService untilService;
	@Autowired
	private FileService fileService;
	@Value("${file.upload-dir}")
	private String filePath;
	
	/**
	 * 获取Ueditor的配置文件
	 * @return
	 */
	@GetMapping("/config")
	public String getConfig() {
	    return "{\n" +
	            "        \"imageActionName\": \"uploadimage\",\n" +
	            "            \"imageFieldName\": \"file\",\n" +
	            "            \"imageMaxSize\": 2048000,\n" +
	            "            \"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"],\n" +
	            "        \"imageCompressEnable\": true,\n" +
	            "            \"imageCompressBorder\": 1600,\n" +
	            "            \"imageInsertAlign\": \"none\",\n" +
	            "            \"imageUrlPrefix\": \"\",\n" +
	            "            \"imagePathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
	            "\n" +
	            "        /* 上传文件配置 */\n" +
	            "        \"fileActionName\": \"uploadfile\",\n" +
	            "            \"fileFieldName\": \"file\",\n" +
	            "            \"filePathFormat\": \"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
	            "            \"fileUrlPrefix\": \"\",\n" +
	            "            \"fileMaxSize\": 51200000,\n" +
	            "            \"fileAllowFiles\": [\n" +
	            "        \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\",\n" +
	            "                \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
	            "                \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\",\n" +
	            "                \".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\",\n" +
	            "                \".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"],\n" +
	            "                \"imageManagerActionName\": \"listimage\", \n" +
	            "                \"imageManagerListPath\": \"/ueditor/jsp/upload/image/\", \n" +
	            "                \"imageManagerListSize\": 20, \n" +
	            "                \"imageManagerUrlPrefix\": \"\", \n" +
	            "                \"imageManagerInsertAlign\": \"none\",\n" +
	            "                \"imageManagerAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"],\n" +
	            "    }";
	}
	
	/**
	 * Ueditor上传文件
	 * 这里以上传图片为例，图片上传后，imgPath将存储图片的保存路径，返回到编辑器中做展示
	 * @param file
	 * @return
	 */
	@PostMapping("/upload")
	public String upload(@RequestParam("file") MultipartFile[] file,String type) {
	    return untilService.ueditorUpload(file,type);
	}
	@RequestMapping("/uploadFile")
	public UploadResult updateFile(MultipartFile file, String type) {
		// Normalize file name
		String fileName = fileService.storeFile(file,type);
		String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath().path("/downloadFile/").path(fileName).toUriString();
		return new UploadResult(fileName, fileDownloadUri, file.getContentType(),fileName, file.getSize());
	}
}
